package com.example.algorithm.mianshi;

import java.util.Arrays;

/**
 * @author: dangwen
 * @createDate: 2021/6/29
 */
public class MaxSubLength {
//    public static void main(String[] args) {
//        long startTime = System.currentTimeMillis(); //获取开始时间
//        String str = "a bc defkdfkhfkgfghkfghfkjgkffg ghi j k lmn op qfdfds fdfdf dsfsdfdsfdf dfdfdfdfdfdfdfd";
//        int i = 0;
//        while (true) {
//            int k = str.indexOf(" ");
//            if (k < 0) {
//                break;
//            }
//            if (i < str.substring(0, k).length()) {
//                i = str.substring(0, k).length();
//            }
//            str = str.substring(k + 1);
//        }
//        System.out.println(i);
//        long endTime = System.currentTimeMillis(); //获取结束时间
//        System.out.println("程序运行时间：" + (endTime - startTime) + "ms");
//        // 得到JVM中的空闲内存量（单位是字节）
//        System.out.println("jvm所剩内存：" + Runtime.getRuntime().freeMemory());
//    }
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis(); //获取开始时间
//        String str = "123# ";
        String str = "a bc defkdfkhfkgfghkfghfkjgkffg ghi j k lmn op qfdfds fdfdf dsfsdfdsfdf dfdfdfdfdfdfdfd";
//        int maxLength = 0;
//        while (true) {
//            int firstIndex = str.indexOf(" ");
//            if (firstIndex < 0) {
//                break;
//            }
////            if (maxLength < str.substring(0, firstIndex).length()) {
////                maxLength = str.substring(0, firstIndex).length();
////            }
//            str = str.substring(firstIndex + 1);
//        }
        int maxLength = getMaxLength2(str);
        System.out.println(maxLength);
        long endTime = System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间：" + (endTime - startTime) + "ms");
        // 得到JVM中的空闲内存量（单位是字节）
        System.out.println("jvm所剩内存：" + Runtime.getRuntime().freeMemory());
    }

    public static int getMaxLength(String strs){
        int maxLength = 0;
        while (true) {
            int firstIndex = strs.indexOf(" ");
            if (firstIndex < 0) {
                break;
            }
            if (maxLength < strs.substring(0, firstIndex).length()) {
                maxLength = strs.substring(0, firstIndex).length();
            }
            strs = strs.substring(firstIndex + 1);
        }
        return maxLength;
    }

    public static int getMaxLength2(String strs){
        String[] s = strs.split(" ");
        if(s.length == 1){
            return strs.trim().length();
        }
        if(s.length == 2){
            return Math.max(s[0].length(), s[1].length());
        }
        int firstIndex = -1;
        int secondIndex = -1;
        int maxLength = 0;
        while (true) {
            if(firstIndex < 0){
                firstIndex = strs.indexOf(" ");
            }
            secondIndex = strs.indexOf(" ",firstIndex + 1);
            if (secondIndex < 0) {
                break;
            }
            int stringLength = secondIndex - firstIndex - 1;
            maxLength = Math.max(maxLength, stringLength);
            firstIndex = secondIndex;

        }
        return maxLength;
    }
}
